<template>
	<view class="register_box">
		<view class="box_header">
			<i class="iconfont icon-meishi" style="font-size: 60rpx; color: #cde6c7;"></i>
			<text>广东美食</text>
		</view>
		<view class="box_center">
			<u--form :rules="rules" :model="userInfo" ref="form">
				<u-form-item prop="phone">
					<u--input placeholder="请输入手机账号" border="bottom" clearable v-model="userInfo.phone"></u--input>
				</u-form-item>
				<u-form-item prop="password">
					<u--input placeholder="请输入密码" border="bottom" clearable type="password" v-model="userInfo.password">
					</u--input>
				</u-form-item>
				<u-form-item prop="rePassword">
					<u--input placeholder="确认密码" border="bottom" clearable type="password"
						v-model="userInfo.rePassword"></u--input>
				</u-form-item>
				<u-form-item prop="username">
					<u--input placeholder="请输入昵称" border="bottom" clearable v-model="userInfo.username"></u--input>
				</u-form-item>
			</u--form>
			<u-button text="注册" color="linear-gradient(to right, rgb(220, 194, 11), rgb(4, 151, 99))" @click="reg">
			</u-button>
		</view>
		<view class="box_footer">
			<text @click="toLogin">已有账号，前往登录</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isRegister: false,
				userInfo: {
					phone: "",
					password: "",
					rePassword: "",
					username: ""
				},
				rules: {
					'phone': [{
							required: true,
							message: '请正确填写手机号',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['blur'],
						}
					],
					'password': {
						type: 'string',
						required: true,
						min: 6,
						message: '密码请不少于六位',
						trigger: ['blur']
					},
					'rePassword': [{
							type: 'string',
							required: true,
							message: '请输入密码',
							trigger: ['blur']
						},
						{
							// 自定义验证函数
							validator: (rule, value, callback) => {
								// 返回true表示校验通过，返回false表示不通过
								return value == this.userInfo.password
							},
							message: '两次密码请一致',
							// 触发器可以同时用blur和change
							trigger: ['blur'],
						}
					],
					'username': {
						type: 'string',
						required: true,
						message: '请输入昵称',
						trigger: ['blur']
					},
				}
			}
		},
		methods: {
			toLogin() {
				this.$emit("isReg", this.isRegister)
			},
			reg() {
				this.$refs.form.validate().then(res => {
					uni.request({
						url: 'http://localhost:8081/user/reg',
						method: 'POST',
						data: this.userInfo,
						header: {
							'Content-Type': 'application/json'
						},
						dataType: 'json',
						success: (res) => {
							if (res.data.state == 200) {
								uni.showToast({
									title: '注册成功',
									duration: 1000,
								});
								setTimeout(() => {
									this.$emit("isReg", this.isRegister)
								}, 500);
							} else {
								uni.$u.toast(res.data.message)
							}
						}
					});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.register_box {
		width: 500rpx;
		height: 720rpx;
		background: #FFFFFF;
		opacity: 0.7;
		margin-top: -100rpx;
		padding: 40rpx 50rpx;

		.box_header {
			text {
				font-size: 50rpx;
				margin-left: 20rpx;
			}
		}

		.box_center {
			margin-top: 30rpx;

			uni-button {
				margin-top: 40rpx;
				margin-bottom: 10rpx;
			}
		}

		.box_footer {
			display: flex;
			justify-content: flex-end;

			text {
				font-size: 20rpx;
			}
		}
	}
</style>
